﻿using PaymentServiceFactory.Services.Implementations;
using PaymentServiceFactory.Services.Interfaces;

namespace PaymentServiceFactory.Services
{
    /// <summary>
    /// 工厂类
    /// </summary>
    //public class PaymentServiceFactorySimple
    //{
    //    private readonly Func<string, IPaymentService> _factory;
    //    public PaymentServiceFactorySimple(Func<string, IPaymentService> factory)
    //    {
    //        _factory = factory;
    //    }
    //    public IPaymentService CreatePaymentService(string serviceType)
    //    {
    //        return _factory(serviceType);
    //    }
    //}

    
    //public class PaymentServiceFactorySimple
    //{
    //    private readonly IServiceProvider _serviceProvider;

    //    public PaymentServiceFactorySimple(IServiceProvider serviceProvider)
    //    {
    //        _serviceProvider = serviceProvider;
    //    }

    //    public IPaymentService Create(string paymentType)
    //    {
    //        // 不再需要传递 orderId 参数
    //        return paymentType switch
    //        {
    //            "CreditCard" => ActivatorUtilities.CreateInstance<CreditCardPayment>(_serviceProvider),
    //            "PayPal" => ActivatorUtilities.CreateInstance<PayPalPayment>(_serviceProvider),
    //            _ => throw new KeyNotFoundException($"未找到支付类型: {paymentType}")
    //        };
    //    }
    //}
    //public class PaymentServiceFactorySimple
    //{
    //    private readonly IServiceScopeFactory _scopeFactory;

    //    public PaymentServiceFactorySimple(IServiceScopeFactory scopeFactory)
    //    {
    //        _scopeFactory = scopeFactory;
    //    }

    //    public IPaymentService Create(string paymentType)
    //    {
    //        using (var scope = _scopeFactory.CreateScope())
    //        {
    //            return paymentType switch
    //            {
    //                "CreditCard" => ActivatorUtilities.CreateInstance<CreditCardPayment>(scope.ServiceProvider),
    //                "PayPal" => ActivatorUtilities.CreateInstance<PayPalPayment>(scope.ServiceProvider),
    //                _ => throw new KeyNotFoundException($"未找到支付类型: {paymentType}")
    //            };
    //        }
    //    }
    //}

    public class PaymentServiceFactorySimple
    {
        private readonly IServiceProvider _serviceProvider;

        public PaymentServiceFactorySimple(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public IPaymentService Create(string paymentType)
        {
            return paymentType switch
            {
                "CreditCard" => ActivatorUtilities.CreateInstance<CreditCardPayment>(_serviceProvider),
                "PayPal" => ActivatorUtilities.CreateInstance<PayPalPayment>(_serviceProvider),
                _ => throw new KeyNotFoundException($"未找到支付类型: {paymentType}")
            };
        }
    }
}
